<?php
/**
 * Created by PhpStorm.
 * User: Lin
 * Date: 2017/3/12 0012
 * Time: 下午 20:39
 */
Class GoodsAction extends Action{
    
    //显示单个商品视图
    public function index(){
        $id=I('id','','intval');
        if(empty($id)){
            redirect(U(GROUP_NAME.'/Index'));
        }

        $hot=M('goods')->where(array('del'=>0,'status'=>3))->limit(0,7)->order('sale_num DESC')->select();
        $goods=M('goods')->where(array('id'=>$id,'del'=>0,'status'=>3,'is_sale'))->find();
        if (empty($goods)){
            $this->error('商品不存在或者已经下架',U(GROUP_NAME.'/Index'));
        }
        import('Class.Category',APP_PATH);
        $cate=M('cate')->where(array('is_show'=>1))->select();
        $this->cateIdArr=Category::findParentsCate($cate,$goods['cate_id']);
        $goods['images']=preg_match_img($goods['images']);
        $store=M('store')->where('is_val=1')->select();
//        p($goods,1);
        $this->hot=$hot;
        $this->store=$store;
        $this->goods=$goods;
        $this->display();
    }

    //显示商品列表视图
    public function showList(){
        $cateId=I('cate_id','','intval');
        if(empty($cateId)){
            redirect(U(GROUP_NAME.'/Index'));
        }
        import('Class.Category',APP_PATH);
        $cate=M('cate')->where(array('is_show'=>1))->select();
        $this->cateArr=Category::findParentsCate($cate,$cateId);

        import('Class.Goods',APP_PATH);
        $cateIdArr=Category::findCatesByCate($cate,$cateId);
        $data= Goods::findGoodsInCateIdArrByOrder($cateIdArr);
        $this->data=$data;
        $this->display();
    }

    //显示商品搜索视图
    public function searchGoods(){
        $keyWord=I('keyWord');
        import('Class.Goods',APP_PATH);
        $data=Goods::searchGoods($keyWord);
        $data['search']=true;
        $this->data=$data;
        $this->display('showList');
    }
}